package com.ssy.lingxi.logistics.serviceimpl;

import com.ssy.lingxi.common.model.dto.UserLoginCacheDTO;
import com.ssy.lingxi.common.response.PageData;
import com.ssy.lingxi.common.response.ResponseCode;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.logistics.api.dto.request.CommonIdRequest;
import com.ssy.lingxi.logistics.entity.ReceiverAddress;
import com.ssy.lingxi.logistics.model.request.ReceiverAddressAgentAddRequest;
import com.ssy.lingxi.logistics.model.request.ReceiverAddressAgentUpdateRequest;
import com.ssy.lingxi.logistics.model.request.ReceiverAddressListRequest;
import com.ssy.lingxi.logistics.model.response.ReceiverAddressAgentListResponse;
import com.ssy.lingxi.logistics.model.response.ReceiverAddressResponse;
import com.ssy.lingxi.logistics.repository.ReceiverAddressRepository;
import com.ssy.lingxi.logistics.service.IReceiverAddressAgentService;
import com.ssy.lingxi.logistics.util.LogisticsAddressUtil;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 代客下单收货地址服务实现类
 * @author yzc
 * @version 2.0.0
 * @date 2020/7/13
 */
@Service
public class ReceiverAddressAgentServiceImpl extends BaseReceiverAddressService implements IReceiverAddressAgentService {

    @Resource
    private ReceiverAddressRepository receiverAddressRepository;

    @Override
    public Wrapper<PageData<ReceiverAddressAgentListResponse>> pageReceiverAddress(ReceiverAddressListRequest request, UserLoginCacheDTO loginUser) {
        // 查询被代理人的收货地址
        Page<ReceiverAddress> pageList = this.pageReceiverAddress(request, request.getMemberId(), request.getRoleId());

        // 组装数据
        List<ReceiverAddressAgentListResponse> resultList = pageList.stream().map(e -> {
            ReceiverAddressAgentListResponse response = new ReceiverAddressAgentListResponse();
            response.setId(e.getId());
            response.setReceiverName(e.getReceiverName());
            response.setProvinceCode(e.getProvinceCode());
            response.setProvinceName(e.getProvinceName());
            response.setCityCode(e.getCityCode());
            response.setCityName(e.getCityName());
            response.setDistrictCode(e.getDistrictCode());
            response.setDistrictName(e.getDistrictName());
            response.setStreetCode(e.getStreetCode());
            response.setStreetName(e.getStreetName());
            response.setAddress(e.getAddress());
            response.setPostalCode(e.getPostalCode());
            response.setAreaCode(e.getAreaCode());
            response.setPhone(e.getPhone());
            response.setTel(e.getTel());
            response.setIsDefault(e.getIsDefault());
            response.setMemberId(e.getMemberId());
            response.setRoleId(e.getRoleId());

            // 拼接完整的地址
            String fullAddress = LogisticsAddressUtil.contractFullAddress(e.getProvinceName(), e.getCityName(), e.getDistrictName(), e.getStreetName(), e.getAddress());
            response.setFullAddress(fullAddress);
            return response;
        }).collect(Collectors.toList());

        return Wrapper.success(new PageData<>(pageList.getTotalElements(), resultList));
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Wrapper<ReceiverAddressResponse> addReceiverAddress(ReceiverAddressAgentAddRequest request, UserLoginCacheDTO loginUser) {
        // 手机校验
        if (!LogisticsAddressUtil.validatePhone(request.getPhone())) {
            return Wrapper.fail(ResponseCode.LS_DATA_PHONE_VALIDATE);
        }

        // 电话校验
//        if (!LogisticsAddressUtil.validateTel(request.getTel())) {
//            return Wrapper.fail(ResponseCode.LS_DATA_TEL_VALIDATE);
//        }

        // 保存
        ReceiverAddress receiverAddress = this.saveReceiverAddressAgent(request);

        ReceiverAddressResponse receiverAddressResponse = this.convertReceiverAddressResponse(receiverAddress);

        return Wrapper.success(receiverAddressResponse);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Wrapper<Void> updateReceiverAddress(ReceiverAddressAgentUpdateRequest request, UserLoginCacheDTO loginUser) {
        // 手机校验
        if (!LogisticsAddressUtil.validatePhone(request.getPhone())) {
            return Wrapper.fail(ResponseCode.LS_DATA_PHONE_VALIDATE);
        }

        // 电话校验
//        if (!LogisticsAddressUtil.validateTel(request.getTel())) {
//            return Wrapper.fail(ResponseCode.LS_DATA_TEL_VALIDATE);
//        }

        ReceiverAddress receiverAddress = receiverAddressRepository.findById(request.getId()).orElse(null);
        if (Objects.isNull(receiverAddress)) {
            return Wrapper.fail(ResponseCode.LS_RECORDS_DON_T_EXIST);
        }

        // 更新
        this.updateReceiverAddress(receiverAddress, request);

        return Wrapper.success();
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Wrapper<Void> deleteReceiverAddress(CommonIdRequest request, UserLoginCacheDTO loginUser) {
        ReceiverAddress receiverAddress = receiverAddressRepository.findById(request.getId()).orElse(null);
        if (Objects.isNull(receiverAddress)) {
            return Wrapper.fail(ResponseCode.LS_RECORDS_DON_T_EXIST);
        }

        receiverAddressRepository.deleteById(receiverAddress.getId());

        return Wrapper.success();
    }
}